package com.yyy.util;

import java.io.*;

/**
 * @Author：yyy
 * @Package：com.yyy.util
 * @Project：HuTuBiill
 * @name：MysqlUtil
 * @Date：2024/11/10 16:13
 * @Filename：MysqlUtil
 * Mysql工具类
 */
public class MysqlUtil {
    //备份sql
    public static void backup(String mysqlPath,String backupfile)throws IOException{
        String commandFormat="\"%s/bin/mysqldump.exe\" -u%s -p%s -hlocalhost -P%d %s -r \"%s\"";
        String command=String.format(commandFormat,mysqlPath,DBUtil.loginName,DBUtil.password,DBUtil.port,DBUtil.database,backupfile);
        Runtime.getRuntime().exec(command);
    }
    //恢复sql
    public static void recover(String mysqlPath,String recoverfile){
        try {
            String commandFormat="\"%s/bin/mysql.exe\" -u%s -p%s %s";
            String command=String.format(commandFormat,mysqlPath,DBUtil.loginName,DBUtil.password,DBUtil.database);
            Process p=Runtime.getRuntime().exec(command);
            OutputStream out=p.getOutputStream();
            String inStr;
            StringBuffer sb=new StringBuffer("");
            String outStr;
            BufferedReader  br=new BufferedReader(new InputStreamReader(new FileInputStream(recoverfile),"utf-8"));
            while ((inStr=br.readLine())!=null){
                sb.append(inStr+"\r\n");
            }
            outStr=sb.toString();
            OutputStreamWriter  writer=new OutputStreamWriter(out,"utf8");
            writer.write(outStr);
            writer.flush();
            out.flush();
            br.close();
            writer.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
